package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private DishFlavorMapper dishFlavorMapper;
	@Autowired
	private SetmealDishMapper setmealDishMapper;

	@Override
	public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

		PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
		Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public DishVO getById(Long id) {
		//DishVO dishVo = dishMapper.getById(id);
		//List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);

		return dishMapper.getById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveDishAndFlavors(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dish.setStatus(StatusConstant.ENABLE);
		//dish.setCreateTime(LocalDateTime.now());
		//dish.setUpdateTime(LocalDateTime.now());
		//dish.setCreateUser(BaseContext.getCurrentId());
		//dish.setUpdateUser(BaseContext.getCurrentId());

		dishMapper.insert(dish);
		//获取insert语句生成的主键值
		Long dishId = dish.getId();

		System.out.println("=======>" + dishId); //
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (flavors != null && !flavors.isEmpty()) {
			flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
		}

		dishFlavorMapper.insertBatch(flavors);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateDishAndFlavors(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		//dish.setUpdateTime(LocalDateTime.now());
		//dish.setUpdateUser(BaseContext.getCurrentId());
		dishMapper.updateById(dish);

		List<DishFlavor> flavors = dishDTO.getFlavors();
		Long dishId = dishDTO.getId();
		dishFlavorMapper.delById(dishId);
		if (flavors != null && !flavors.isEmpty()) {
			//dishFlavorMapper.delById(dishId);
			flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
			dishFlavorMapper.insertBatch(flavors);
		}


	}

	@Override
	public void delete(List<Long> ids) {
		//判断是否是启售，如果是不能删除，并报异常
		ids.forEach(id -> {
			if (Objects.equals(dishMapper.getById(id).getStatus(), StatusConstant.ENABLE))
				throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
		});

		//setmealDishMapper.queryByIds(ids);
		ids.forEach(id -> {
			Integer count = dishMapper.queryWithSetmealCountByIds(id);
			if (count > 0) {
				throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
			}
		});


		ids.forEach(id -> dishMapper.deleteById(id));

	}

	@Override
	public void startOrStop(Integer status, Long id) {
		dishMapper.startOrStop(status, id);
	}

	@Override
	public List<DishVO> list(Dish dish) {
		return dishMapper.list(dish);
	}


}
